package RockPaperScissors
{
    public class Processor implements IProcessable
    {
        protected var _onComplete:Function;
        protected var _onFail:Function;
        protected var _workflow = [];
        
        public function Processor(onComplete:Function = null, onFail:Function = null)
        {
        }
        
        protected function setupProcessor(onComplete:Function, onFail:Function, processSteps:*)
        {
            this._onComplete = onComplete;
            this._onFail = onFail;
            this._workflow = processSteps;
        }

        public function Process(data)
        {
            var taskType:Class = this._workflow.pop() as Class;
            
            new taskType(this.ProcessingComplete, this._onFail).Process(data);
        }

        ///This will be invoked by one of the processors.
        ///If there are processors that haven't run yet then
        ///this function will process those instead of 
        ///running onComplete.
        private function ProcessingComplete(data)
        {
            if(this._workflow.length > 0)
            {
                this.Process(data);
                
                return;
            }
            
            this._onComplete(data);
        }
        
        
    }
}